package tests.smoke.ppa;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import framework.BaseForm;
import framework.BaseTest;
import framework.DataBaseUtils;
import framework.GmailUtils;
import framework.catalog.CatalogWithTreeForm;
import framework.catalog.CatalogWithoutTreeForm;
import framework.entities.Organizaciya;
import framework.entities.SerializeEntity;
import framework.entities.Organizaciya.ORGANIZATION;
import framework.entities.SerializeEntity.Entities;
import framework.forms.LoginForm;
import framework.forms.MainForm;
//import framework.forms.MyCabinetForm;
import framework.forms.OrganizationRegisterForm;
//import framework.forms.ProsmotrSvedeniyObOrganizaciiFrom;
//import framework.forms.UserRegistrationDataForm;
import framework.forms.BaseOrganizationForm.ORG_DATA;
//import framework.forms.BaseOrganizationForm.USER_DATA;
//import framework.forms.UserRegistrationDataForm.USER_REGISTRATION_INFO;

/**  223FZ-3602:ЛК (ПАК): Регистрация организации (Заказчик)
 */
public class MyRegistraciyaOrganizaciiOnly extends BaseTest{

	/** Тест.
	 * @param expectedMessageSender адрес отправителя
	 * письма для подтверждения
	 * @param email почта куда будет отправлено письмо для подтверждения
	 * @param password пароль к почте
	 * @param attach1 наименование прикрепляемого документа 1
	 * @param attach2 наименование прикрепляемого документа 2
	 * @param attach3 наименование прикрепляемого документа 3
	 * @param postalAddress адрес регистрируемой организации
	 * @param contactSecondName фамилия контактного лица
	 * @param contactFirstName имя конт. лица
	 * @param contactMiddleName отчество конт. лица
	 * @param countryCode код страны
	 * @param cityCode код города
	 * @param number номер тел./факса
	 * @param ext расширенный код
	 * @param additionalInfo дополнительная инфа
	 * @param webSite веб сайт организации
	 * @param vidYurLica вид юридического лица
	 * @param path путь к прикрепляемому файлу
	 * @param okvedId часть кода ОКВЕД
	 * @param okvedType тип ОКВЕД
	 * @param timeZone часовой пояс
	 * @param okpoCode код ОКПО
	 */
	@Test
	@Parameters({"expectedMessageSender","email","password",
		"attach1","attach2", "attach3","postalAddress","contactSecondName", "contactFirstName",
		"contactMiddleName", "countryCode",	"cityCode", "number", "ext", "additionalInfo",
		"webSite", "vidYurLica", "path", "okvedId","okvedType", "timeZone", "okpoCode"})
	public void runTest(final String expectedMessageSender, final String email,
			final String password,final String attach1,
			final String attach2,
			final String attach3,final String postalAddress,
			final String contactSecondName,	final String contactFirstName,
			final String contactMiddleName,final String countryCode,
			final String cityCode, final String number,
			final String ext, final String additionalInfo,
			final String webSite, final String vidYurLica,
			final String path, final String okvedId,
			final String okvedType, final String timeZone, String okpoCode){

		//Удаление записи из таблицы
		DataBaseUtils db = new DataBaseUtils();
		//Удаление писем с почтового ящика
		GmailUtils gmail = new GmailUtils();
		gmail.deleteAllMessages(email, password);

		logStep(); // 1
		MainForm mainForm = new MainForm();
		String appVersion = BaseForm.getVersion();
		//db.deleteOrganizationByName( "MY_Testova9 Organizazi9 \"T4UP AUTO 01\"", appVersion);
		LoginForm loginForm  = mainForm.openLoginForm();
		OrganizationRegisterForm orf = loginForm.loginAsNotRegister();
		String fullName = orf.getOrgData(ORG_DATA.FULL_NAME);
		String ogrn = orf.getOrgData(ORG_DATA.OGRN);
		String inn = orf.getOrgData(ORG_DATA.INN);
		String kpp = orf.getOrgData(ORG_DATA.KPP);

		logStep(); // 2
		orf.selectTimeZone(timeZone);

		String okatoCode = "";
		String oktmoCode = "";
		String okfsCode = "";
		String okopfCode = "";
		logStep(); // 3
		if(orf.isOKPOMode()){
			StringBuilder okato = new StringBuilder();
			StringBuilder oktmo = new StringBuilder();
			StringBuilder okfs = new StringBuilder();
			StringBuilder okopf = new StringBuilder();
			okpoCode = orf.selectOkpoCode(okato, oktmo, okfs, okopf);
			okatoCode = okato.toString();
			oktmoCode = oktmo.toString();
			okfsCode = okfs.toString();
			okopfCode = okopf.toString();
			orf.assertClassificationAttributes(okpoCode, okatoCode.toString(), oktmoCode.toString(), okfsCode.toString(), okopfCode.toString());
		}else{
			logStep();
			orf.setOkpoCode(okpoCode);

			logStep(); // 4
			CatalogWithoutTreeForm swtf= orf.openOkatoSpavochnik();
			okatoCode = swtf.getCode();
			swtf.selectValueAndWaitForCloseWnd();
			orf.assertOkatoCode(okatoCode);

			logStep(); // 5
			swtf= orf.openOktmoSpavochnik();
			oktmoCode = swtf.getCode();
			swtf.selectValueAndWaitForCloseWnd();
			orf.assertOktmoCode(oktmoCode);

			logStep(); // 6
			CatalogWithTreeForm sf = orf.openOkfsSpavochnik();
			okfsCode = sf.getRandomItemCode();
			sf.selectRandomItemWithoutOpeningTree();
			orf.assertOkfsCode(okfsCode);

			logStep(); // 7
			sf = orf.openOkopfSpavochnik();
			okopfCode = sf.getRandomItemCode();
			sf.selectRandomItemWithoutOpeningTree();
			orf.assertOkopfCode(okopfCode);
		}

		logStep(); // 8
		String okvedCode = orf.selectOkved(okvedId, okvedType);
		orf.assertOkved(okvedCode, true);

		logStep(); // 9
		orf.setPostalAddress(postalAddress);
		orf.setEmail(email);
		orf.setWebSite(webSite);
		orf.setContactName(contactSecondName, contactFirstName, contactMiddleName);
		orf.setContactEmail(email);
		orf.setPhone(countryCode, cityCode, number, ext);
		orf.setFax(countryCode, cityCode, number, ext);
		orf.setAdditionalInfo(additionalInfo);

		logStep(); // 10
		orf.selectVidYuLica(vidYurLica);

		logStep(); // 11
		orf.attachFile(attach1, path, attach1);
		orf.attachFile(attach2, path, attach2);
		orf.attachFile(attach3, path, attach3);

		logStep(); // 12
		orf.clickNextBtn();
		/*String lastName = orf.getUserData(USER_DATA.LAST_NAME);
		String firstName = orf.getUserData(USER_DATA.FIRST_NAME);
		String secondName = orf.getUserData(USER_DATA.SECOND_NAME);
		String dolzhnost = orf.getUserData(USER_DATA.DOLZHNOST);
		String login = orf.getUserData(USER_DATA.LOGIN);
		String polnomochiya = orf.getUserData(USER_DATA.POLNOMOCHIYA);*/

		logStep(); //13
		orf.zaregOrg(email);

		logStep(); //14
		orf.getConfirmMessageAndVerifyRegIsComplete(email, password, expectedMessageSender);

		// Установка пароля для пользователя
		new DataBaseUtils().setPassword(System.getProperty(LOGIN_UNREG_ORG));

		/*logStep(17); // 17
		MyCabinetForm mc = new MyCabinetForm();
		ProsmotrSvedeniyObOrganizaciiFrom psoof = mc.openOrgData();
		psoof.assertOrgStatus("Зарегистрирована");		psoof.assertOrgData(ORG_DATA.FULL_NAME, fullName);
		psoof.assertOrgData(ORG_DATA.OGRN, ogrn);		psoof.assertOrgData(ORG_DATA.INN, inn);
		psoof.assertOrgData(ORG_DATA.KPP, kpp);		psoof.assertOrgData(ORG_DATA.ADDRESS, address);
		psoof.assertTimeZone(timeZone);		psoof.assertOkatoCode(okatoCode);
		psoof.assertOktmoCode(oktmoCode);		psoof.assertOkfsCode(okfsCode);
		psoof.assertOkopfCode(okopfCode);		psoof.assertOkved(okvedCode, true);
		psoof.assertPostal(postalAddress);		psoof.assertEmail(email);
		psoof.assertWebSite(webSite);		psoof.assertContactFace(contactSecondName, contactFirstName, contactMiddleName);
		psoof.assertContactEmail(email);		psoof.assertPhone(countryCode, cityCode, number, ext);
		psoof.assertFax(countryCode, cityCode, number, ext);		psoof.assertAdditionalInfo(additionalInfo);
		psoof.openPolnomochiyaTab();		psoof.assertYurLico(vidYurLica);
		psoof.assertAttachedFile(attach1);		psoof.assertAttachedFile(attach2);
		psoof.assertAttachedFile(attach3);		//Проверка всех полей на форме регистрационных данных пользователя
		UserRegistrationDataForm urf =  mc.openRegistrationUserInfo();
		USER_REGISTRATION_INFO[] user_fields = new USER_REGISTRATION_INFO[]{
				USER_REGISTRATION_INFO.LASTNAME, USER_REGISTRATION_INFO.FIRSTNAME, USER_REGISTRATION_INFO.MIDDLENAME,
				USER_REGISTRATION_INFO.DOLZHNOST, USER_REGISTRATION_INFO.EMAIL, USER_REGISTRATION_INFO.LOGIN, USER_REGISTRATION_INFO.POLNOMOCHIYA
		};		String[] userinfo = new String[]{
				lastName, firstName, secondName, dolzhnost, email, login, polnomochiya
		};		urf.assertUserRegInfo(user_fields, userinfo);*/

		String status  = "Завершена";
		//Сериализация данных об организации
		String[] attachedFiles = {attach1, attach2, attach3};
		ORGANIZATION[] orgItems = {
				ORGANIZATION.STATUS, ORGANIZATION.FULL_NAME, ORGANIZATION.OGRN,
				ORGANIZATION.INN, ORGANIZATION.KPP,	ORGANIZATION.TIMEZONE, ORGANIZATION.OKATO_CODE, ORGANIZATION.OKTMO_CODE,
				ORGANIZATION.OKFS_CODE, ORGANIZATION.OKOPF_CODE,
				ORGANIZATION.OKVED_CODE, ORGANIZATION.POSTAL_ADDRESS, ORGANIZATION.EMAIL, ORGANIZATION.WEB_SITE,
				ORGANIZATION.CONTACT_SECONDNAME, ORGANIZATION.CONTACT_FIRSTNAME, ORGANIZATION.CONTACT_MIDDLENAME,
				ORGANIZATION.COUNTRY_CODE, ORGANIZATION.CITY_CODE, ORGANIZATION.NUMBER,
				ORGANIZATION.EXT_CODE, ORGANIZATION.ADDITIONAL_INFO, ORGANIZATION.VID_YUR_LICA};
		String[] values = {status, fullName, ogrn, inn, kpp, timeZone, okatoCode, oktmoCode, okfsCode, okopfCode,
				okvedCode, postalAddress, email, webSite, contactSecondName, contactFirstName, contactMiddleName, countryCode,
				cityCode, number, ext, additionalInfo, vidYurLica};
		Organizaciya organization = new Organizaciya(orgItems, values, attachedFiles);
		SerializeEntity.serializeEntity(Entities.ORGANIZATION_MAIN, organization);
		logger.info("Тест пройден успешно!");
	}
}
